module.exports = class {
    constructor() {
        this.$max = 10000;
        this.$buffer = [];
        this.$cache = {}
    }
    $beforeEach({
        from,
        target,
        args,
        id,
        fns
    }) {
        try {
            const data = this.$cache[`${id}_${from}_${target}`] = {}
            if (this.$buffer.length > this.$max) {
                this.$buffer.shift();
            }
            data.args = args;
            data.from = from;
            data.to = target;
            data.fns = fns;
            if (typeof performance !== 'undefined') {
                data.startTime = performance.now();
            } else {
                data.startTime = Date.now();
            }
        } catch (e) {
            console.error(e)
        }
    }
    $afterEach({
        val,
        from,
        target,
        id,
        topo,
        error
    }) {
        try {
            let data = this.$cache[`${id}_${from}_${target}`];
            if(!data){
                data = {
                    from,
                    to: target,
                    startTime: typeof performance !== 'undefined' ? performance.now() : Date.now(),
                }
            }
            // console.log('afterEach',data)
            data.results = val;
            data.id = id;
            data.topo = topo;
            data.status = 'running'
            if (typeof performance !== 'undefined') {
                data.endTime = performance.now();
            } else {
                data.endTime = Date.now();
            }
            if(error){
                data.error = error;
            }
            this.$buffer.push(data);
        } catch (e) {
            console.error(e)
        }
    }
    $destroy({
        id,
        from,
        target,
    }){
        this.$buffer.push({
            id,
            from,
            target,
            status: 'stopped',
        })
    }
    $getBuffer() {
        const buffer = this.$buffer;
        this.$buffer = [];
        return buffer;
    }
    $getBufferLength(){
        return this.$buffer.length;
    }

}